package action.admin;

import service.securite.DroitsAcces;
import service.securite.Erreur;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bdd.BddClientSelectRequest;
import bean.client.Client;

import bean.dossier.*;

/**
 * Servlet implementation class AdminGestionCompteAdmin
 */
public class AdminGestionCompteServiceAction extends Action {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminGestionCompteServiceAction() {
        super();
    }

    public static boolean aLesDroits(HttpServletRequest request) {
    	return (DroitsAcces.isAdminCompte(request) || DroitsAcces.isSuperAdmin(request));
    }

	protected void initSession(HttpServletRequest request )
	{
		HttpSession session = request.getSession();
	}	
	
	public ActionForward execute(final ActionMapping pMapping,
			ActionForm pForm, final HttpServletRequest request,
			final HttpServletResponse pReponse) {
		ActionErrors errors = new ActionErrors();
		this.initSession(request);
		
		if (!aLesDroits(request)) {
			//Erreur.setErreurDroitsAdmin(request, response, getServletContext());
			errors.add("errors",new ActionMessage("errors.admin"));
			saveErrors(request,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
		
		ArrayList<Client> listClient = null;		
		String mail = "";
		String nomPrenom = "";
		String bloque = "";
		String min = "0";
		if ( request.getParameter("min") != null )
			min = request.getParameter("min");
		
		if ( request.getParameter("action") != null && request.getParameter("action").equals("recherche") ){
			mail = request.getParameter("mail");
			nomPrenom = request.getParameter("nomPrenom");
			bloque = request.getParameter("bloque");
			try {
				listClient = BddClientSelectRequest.getClientsRecherche(mail, nomPrenom, true, bloque);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errors.add("errors",new ActionMessage("errors.exception"));
				saveErrors(request,errors); 
				return (new ActionForward(pMapping.getInput()));
			}			
		} else{
			try {
				listClient = BddClientSelectRequest.getComptesClient(true);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errors.add("errors",new ActionMessage("errors.exception"));
				saveErrors(request,errors); 
				return (new ActionForward(pMapping.getInput()));
			}
		}
		
		request.setAttribute("listClients", listClient);
		request.setAttribute("typeCompte", "services");
		request.setAttribute("mail", mail);
		request.setAttribute("nomPrenom", nomPrenom);
		request.setAttribute("bloque", bloque);
		request.setAttribute("min", min);
		
		/*
		String url = "/"+Dossier.getDossierAdmin(false)+"gestioncompteclient.jsp";
		ServletContext context = getServletContext();
		RequestDispatcher rd = context.getRequestDispatcher(url);
		rd.forward(request, response);*/
		return pMapping.findForward("success");
	}
}
